/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorcreatorgen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.entity.accessor.base.IAccessor;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.ManagerGeneratorContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.ThrowStatement;

//IAccessor CreateReadonlyAccessor(ICefData inner);
public class CreateChildReadonlyAccessorGenerator extends ClassMethodGenerator
{
	protected ManagerGeneratorContext parentContext;
	public CreateChildReadonlyAccessorGenerator(ManagerGeneratorContext parentContext)
	{
		this.parentContext = parentContext;
	}


	@Override
	protected String getMethodName(){return "createChildReadonlyAccessor";}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(IAccessor.class.getTypeName());
	}

	protected static final String ParName_Code = "code";
	protected static final String ParName_Data = "inner";
	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		ArrayList<ParameterInfo> list =new ArrayList<ParameterInfo>();
		ParameterInfo parameterInfo=new ParameterInfo();
		parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
		parameterInfo.setParamName(ParName_Code);
		list.add(parameterInfo);
		parameterInfo=new ParameterInfo();
		parameterInfo.setParamType(new TypeInfo(ICefData.class.getTypeName()));
		parameterInfo.setParamName(ParName_Data);
		list.add(parameterInfo);
		return list;
	}
	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<Modifier.ModifierKeyword>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}
	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		SwitchStatement switchStatement=ast.newSwitchStatement();
		addRootNodeCaseStatement(switchStatement);
		addChildCaseStatements(switchStatement);

		addDefaultCaseStatement(switchStatement);
		block.statements().add(switchStatement);
		return block;
	}

	private void addDefaultCaseStatement(SwitchStatement switchStatement)
	{
//		SetExceptionCode setExceptionCode =new SetExceptionCode();
//		setExceptionCode.addExceptionCodeMessage(switchStatement,ast,"找不到节点，节点编号：",ParName_Code);
		SwitchCase switchCase=ast.newSwitchCase();
		switchCase.setExpression(null);
		switchStatement.statements().add(switchCase);
		addImport(CefException.class.getTypeName());
		ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"找不到节点，节点编号：",ParName_Code,true);
//		ClassInstanceCreation classInstanceCreation =ast.newClassInstanceCreation();
//		setExceptionCode.setClassInstanceCreation(classInstanceCreation,ast,"找不到节点，节点编号：",ParName_Code);
//		classInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//		InfixExpression infixExpression=ast.newInfixExpression();
//		infixExpression.setOperator(InfixExpression.Operator.PLUS);
//		StringLiteral stringLiteral =ast.newStringLiteral();
//		stringLiteral.setLiteralValue("找不到节点，节点编号：");
//		infixExpression.setLeftOperand(stringLiteral);
//		infixExpression.setRightOperand(ast.newSimpleName(ParName_Code));
//		classInstanceCreation.arguments().add(infixExpression);
		//throwStatement.setExpression(classInstanceCreation);
		switchStatement.statements().add(throwStatement);
	}

	private void addChildCaseStatements(SwitchStatement switchStatement)
	{
		HashMap<String, MediateType> childTypes = parentContext.getProjContext().getReadonlyAccessorMediateTypes();
		if (childTypes == null || childTypes.isEmpty()) {
			return;
		}

		for (Map.Entry<String, MediateType> item : childTypes.entrySet()) {
			addChildCaseStatement(item.getKey(), item.getValue(), switchStatement);
		}
	}

	private void addChildCaseStatement(String objectCode, MediateType objectType, SwitchStatement switchStatement)
	{
		if (parentContext.getProjContext().getEntityDataMediateTypes().containsKey(objectCode) == false) {
			return;
		}
		if (objectType.getClassName().endsWith("Accessor") == false) {
			return;
		}
		SwitchCase switchCase=ast.newSwitchCase();
		StringLiteral stringLiteral=ast.newStringLiteral();
		stringLiteral.setLiteralValue(objectCode);
		switchCase.setExpression(stringLiteral);
		switchStatement.statements().add(switchCase);

		ReturnStatement returnStatement=ast.newReturnStatement();
		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newSimpleType(ast.newName(objectType.getFullName())));

		CastExpression castExpression=ast.newCastExpression();
		castExpression.setType(ast.newSimpleType(ast.newName(parentContext.getProjContext().getEntityDataMediateTypes().get(objectCode).getFullName())));
		castExpression.setExpression(ast.newSimpleName(ParName_Data));
		classInstanceCreation.arguments().add(castExpression);
		if(parentContext.getProjContext().getUseDualAcc()) {
			classInstanceCreation.arguments().add(ast.newBooleanLiteral(true));
		}

		returnStatement.setExpression(classInstanceCreation);
		switchStatement.statements().add(returnStatement);
	}

	private void addRootNodeCaseStatement(SwitchStatement switchStatement) {
		switchStatement.setExpression(ast.newSimpleName(ParName_Code));
	}
}
